package com.steven.ourcontest.collect.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.collect.api.CollectApi
import com.steven.ourcontest.contest.bean.MatchCollectionRes
import com.steven.ourcontest.utils.plusAssign

class MatchCollectionViewModel : BaseViewModel() {
    private val _matchCollectionsRes = MutableLiveData<MatchCollectionRes>()
    val matchCollectionsRes: LiveData<MatchCollectionRes>
        get() = _matchCollectionsRes

    private var lastCreateTime = Long.MAX_VALUE
    private fun queryMatchCollections(success: (() -> Unit)? = null, error: (() -> Unit)? = null) {
        disposed += CollectApi.instance.selectMatchCollections(lastCreateTime)
            .subscribe({
                success?.invoke()
                _matchCollectionsRes.value = it
                lastCreateTime = it.data.last().createTime
            }, {
                it.printStackTrace()
                error?.invoke()
            })
    }

    fun refresh(success: (() -> Unit)? = null, error: (() -> Unit)? = null) {
        lastCreateTime = Long.MAX_VALUE
        queryMatchCollections(success, error)
    }

    fun loadMore(success: (() -> Unit)? = null, error: (() -> Unit)? = null) {
        queryMatchCollections(success, error)
    }
}